package com.group.elmboot.po;

import java.math.BigDecimal;

import com.baomidou.mybatisplus.annotations.TableId;
import com.group.elmboot.util.CommonUtil;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Wallet {
	
	@TableId
	private String userId;
	private BigDecimal balance;
	private BigDecimal frozenAmount;
	private String createTime;
	private BigDecimal overdraftLimit;
	private String balanceLastModifiedTime;
	private boolean isAllowedOverdraft;
	private boolean isOverdraft;
	private String overdraftTime;
	
	public Wallet(String userId) {
		this.userId =userId;
		balance=BigDecimal.ZERO;
		overdraftLimit=BigDecimal.ZERO;
		frozenAmount=BigDecimal.ZERO;
		isAllowedOverdraft=false;
		isOverdraft=false;
	}
	
	public void increaseFrozenAmount(BigDecimal amount) throws Exception {
		if(amount.compareTo(BigDecimal.ZERO) < 0) {
			throw new Exception("冻结金额不能为负数！");
		}
		frozenAmount=frozenAmount.add(amount);
	}

	public void unfreeze(BigDecimal amount) throws Exception {
		if(amount.compareTo(BigDecimal.ZERO) < 0) {
			throw new Exception("解冻金额不能为负数！");
		}
		frozenAmount=frozenAmount.subtract(amount);
		increaseBalance(amount);
	}
	
	public void increaseOverdraftAmount(BigDecimal amount) throws Exception {
		if(amount.compareTo(BigDecimal.ZERO) < 0) {
			throw new Exception("增加透支限额不能为负数！");
		}
		overdraftLimit=overdraftLimit.add(amount);
	}
	
	public void decreaseOverdraftAmount(BigDecimal amount) throws Exception {
		if(amount.compareTo(BigDecimal.ZERO) < 0) {
			throw new Exception("减少透支限额不能为负数！");
		}
		overdraftLimit=overdraftLimit.subtract(amount);
	}
	
	public void closeOverdraft() {
		isAllowedOverdraft=false;
	}
	
	public void openOverdraft() {
		isAllowedOverdraft=true;
	}

	public void increaseBalance(BigDecimal increasedAmount) throws Exception {
		if(increasedAmount.compareTo(BigDecimal.ZERO) < 0) {
			throw new Exception("增加余额不能为负数！");
		}
		if(balance.negate().compareTo(increasedAmount) < 0 && balance.compareTo(BigDecimal.ZERO) < 0) {
			isOverdraft=false;
		}
		balance=balance.add(increasedAmount);
		balanceLastModifiedTime = CommonUtil.getCurrentDate();
	}
	
	private BigDecimal getAvailableAmount() {
		BigDecimal available=balance;
		if(isAllowedOverdraft)
			available=available.add(overdraftLimit);
		return available;
	}
	
	public void decreaseBalance(BigDecimal decreasedAmount) throws Exception {
		if(decreasedAmount.compareTo(BigDecimal.ZERO) < 0) {
			throw new Exception("减少余额不能为负数！");
		}
		if(decreasedAmount.compareTo(getAvailableAmount()) > 0) {
			throw new Exception("余额不足/透支余额超限！");
		}
		if(balance.compareTo(decreasedAmount) < 0 && balance.compareTo(BigDecimal.ZERO) >= 0) {
			isOverdraft=true;
			overdraftTime=CommonUtil.getCurrentDate();
		}
		balance=balance.subtract(decreasedAmount);
		balanceLastModifiedTime = CommonUtil.getCurrentDate();
	}
	
	public void interest(BigDecimal rateDayly) throws Exception {
		if(rateDayly.compareTo(BigDecimal.ZERO) < 0) {
			throw new Exception("日息率不能为负数！");
		}
		balance=balance.add(balance.multiply(rateDayly));
	}
	
}
